﻿using System;
using System.Windows;
using UtilZ.Dotnet.Ex.Log;
using UtilZ.Dotnet.Ex.Model;
using UtilZ.Dotnet.WindowsDesktopEx.WPF;

namespace DFWpfApp1
{
    /// <summary>
    /// TestComboBoxMultiSelectemItemsSource.xaml 的交互逻辑
    /// </summary>
    public partial class TestComboBoxMultiSelectemItemsSource : Window
    {
        public TestComboBoxMultiSelectemItemsSource()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var redirectAppenderToUI = (RedirectAppender)Loger.GetAppenderByName(null, "RedirectToUI");
            if (redirectAppenderToUI != null)
            {
                redirectAppenderToUI.RedirectOuput += RedirectLogOutput;
            }
        }

        private void RedirectLogOutput(object sender, RedirectOuputArgs e)
        {
            logControl.AddLog(e.Item.Content, e.Item.Level);

            //string str;
            //try
            //{
            //    str = string.Format("{0} {1}", DateTime.Now, e.Item.Content);
            //}
            //catch (Exception ex)
            //{
            //    str = ex.Message;
            //}

            //logControl.AddLog(str, e.Item.Level);
        }
    }

    internal class TestComboBoxMultiSelectemItemsSourceVM : NotifyPropertyChangedAbs
    {
        private readonly ProjectInfo _all;

        private readonly ComboBoxMultiSelectemItemsSource<ProjectInfo> _projectInfos;
        public ComboBoxMultiSelectemItemsSource<ProjectInfo> ProjectInfos
        {
            get { return _projectInfos; }
        }

        public TestComboBoxMultiSelectemItemsSourceVM()
            : base()
        {
            this._all = new ProjectInfo("全部", 18, true);

            this._projectInfos = new ComboBoxMultiSelectemItemsSource<ProjectInfo>("未选", this._all, this.CheckedChanged);
            //this._projectInfos.ManualCommitCollectionChanged();
            //this._projectInfos.Add(new ProjectInfo("yf", 23, false));
            //this._projectInfos.Add(new ProjectInfo("zx", 25, false));
            //this._projectInfos.Add(new ProjectInfo("yc", 20, false));

            Random rnd = new Random();
            for (int i = 0; i < 30; i++)
            {
                this._projectInfos.Add(new ProjectInfo($"yy_{i}", rnd.Next(15, 26), false));
            }
            //this._projectInfos.AutoCommitCollectionChanged();

            //List<ProjectInfo> list = new List<ProjectInfo>();
            //list.Add(this._all);
            //list.Add(new ProjectInfo("yf", 23, true));
            //list.Add(new ProjectInfo("zx", 25, true));
            //list.Add(new ProjectInfo("yc", 20, true));
            //list.Add(new ProjectInfo("yy", 18, true));
            //this._projectInfos = new ComboBoxMultiSelectemItemsSource<ProjectInfo>(this._all, list);

            // this._projectInfos.CheckedChanged += _projectInfos_CheckedChanged;
        }

        private void CheckedChanged(CheckedArgs<ProjectInfo> e)
        {
            Loger.Info($"{e.Source.Text}    {e.NewChecked.ToString()}");
        }

        private void _projectInfos_CheckedChanged(object sender, CheckedArgs<ProjectInfo> e)
        {
            this.CheckedChanged(e);
        }
    }

    internal class ProjectInfo : NotifyPropertyChangedAbs, ICheckItem
    {
        private bool _isChecked = false;
        public bool IsChecked
        {
            get { return _isChecked; }
            set
            {
                this.SetIsChecked(value, true);
            }
        }

        /// <summary>
        /// 设置是否选中值
        /// </summary>
        /// <param name="isChecked">是否选中[true:选中;false:未选中]</param>
        /// <param name="raiseNotifyPropertyChanged">是否触发属性改变事件[true:触发;false:不触发]</param>
        public void SetIsChecked(bool isChecked, bool raiseNotifyPropertyChanged)
        {
            if (_isChecked == isChecked)
            {
                return;
            }

            bool oldChecked = _isChecked;
            _isChecked = isChecked;
            base.OnRaisePropertyChanged(nameof(IsChecked));

            if (raiseNotifyPropertyChanged)
            {
                this._checkedChanged?.Invoke(this, new CheckedArgs<ICheckItem>(this, oldChecked, isChecked));
            }
        }

        private string _name;
        public string Text
        {
            get { return _name; }
            set
            {
                _name = value;
                base.OnRaisePropertyChanged();
            }
        }

        private int _age = 10;
        public int Age
        {
            get { return _age; }
            set
            {
                _age = value;
                base.OnRaisePropertyChanged();
            }
        }


        public string ImageFilePath { get; set; }


        private event EventHandler<CheckedArgs<ICheckItem>> _checkedChanged;
        private bool _addCheckedChanged = false;
        public event EventHandler<CheckedArgs<ICheckItem>> CheckedChanged
        {
            add
            {
                if (this._addCheckedChanged)
                {
                    return;
                }

                _checkedChanged += value;
                this._addCheckedChanged = true;
            }
            remove
            {
                if (this._addCheckedChanged)
                {
                    _checkedChanged -= value;
                    this._addCheckedChanged = false;
                }
            }
        }

        public ProjectInfo(string name, int age, bool isChecked)
           : base()
        {
            this._name = name;
            this._age = age;
            this._isChecked = isChecked;
        }

        public override string ToString()
        {
            return _name;
        }
    }
}
